package com.demo.demo;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.val;
import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbMakerConfigException;
import org.lionsoul.ip2region.DbSearcher;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Copyright
 *
 * @Author WuHuang
 * @Date 2022/7/25
 */

public class CdpTest {

    private static String session_url = "https://prod-api.nissinfoodium.com.cn/cdp-data-process/job/crs_session_event_data_v1";
//    private static String regular_url = "https://prod-api.nissinfoodium.com.cn/cdp-data-process/job/regular_data";
    private static String activity_url = "https://prod-api.nissinfoodium.com.cn/cdp-data-process/job/activity_data";

    public static void main(String[] args) throws InterruptedException, DbMakerConfigException, IOException {
//        System.out.println(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
//        HashMap<String, Object> body = Maps.newHashMap();
//        body.put("data_date","2022-12-02");
//        String s = HttpUtil.get(activity_url,body);
//        System.out.println(s);
//        ArrayList<String> strings = Lists.newArrayList("2022-12");
//        for (String string : strings) {
//            List<String> monthDays = getMonthDays(string);
//
//            Integer a = 0;
//            for (String day : monthDays) {
//                HashMap<String, Object> body = Maps.newHashMap();
//                body.put("data_date",day);
//                String s = HttpUtil.get(activity_url,body);
//                if (StrUtil.isNotEmpty(s)){
//                    System.out.println(day);
//                    JSONObject re = JSONObject.parseObject(s);
//                    String message = re.getString("message");
//                    if ("success".equals(message)){
//                        a++;
//                    }
//                }
//                System.out.println(s);
//                Thread.sleep(1000 * 10);
//            }
//            System.out.println("执行成功" + a);
//        }
        String ip = "39.144.98.156";
        DbSearcher searcher = new DbSearcher(new DbConfig(), "D:\\WorkSpace\\wu\\spring-boot-study\\springboot-demo\\src\\main\\resources\\ip2region.db");
        DataBlock dataBlock = searcher.btreeSearch(ip);
        // 格式为：国家|区域|省份|城市|运营商
        String address = dataBlock.getRegion();
        System.out.println(address);


    }

    public static List<String> getMonthDays(String billingCycle) {
        List<String> monthDays = new ArrayList<String>();
        int startNum = 1;
        LocalDate localDate = LocalDate.parse(billingCycle + "-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println("localDate为：" + localDate);
        int lastNum = localDate.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();
        System.out.println("lastNum为：" + lastNum);
        Integer firstDay = new Integer(localDate.format(DateTimeFormatter.BASIC_ISO_DATE));
        System.out.println("firstDay为：" + firstDay);
        for (int i = startNum; i <= lastNum; i++) {
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
            LocalDate parse = LocalDate.parse(firstDay.toString(),dtf);
            String dateStr = parse.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            monthDays.add(dateStr);
            firstDay++;
        }
        System.out.println("monthDays为：" + monthDays);
        return monthDays;
    }


}
